#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   数据库脏数据跟新.py    
@Contact :   fengfeng.qiu@amh-group.com

@Modify Time      @Author    
------------      -------    
2022/5/16 17:26   qiufengfeng      
'''

from qiu_utils.db_utils.mysql_utls import MysqlConnector
import requests
import json
import ast
mysql_info = {
    'host':"10.190.22.85",
    "port":3306,
    "user":"root",
    'passwd':"root",
    'db':"contraband",
    "charset":"utf8"
}

table_name = "biaozhu_contraband_new"

mysql_instance = MysqlConnector(mysql_info,table_name)

word_list = []
def get_doccano_result(item):
    text = item['text']
    biaozhu_url = "https://dev-beetle.amh-group.com/beetle/contraband/autoDoccano"
    data = {"text": text}
    header = {"Content-Type": "application/json"}
    try:
        req = requests.post(biaozhu_url,data=json.dumps(data), headers=header)
        check_label_keys = []
        for i in req.json():
            check_label_keys.append((i['start_offset'],i["end_offset"]))

        if not check_label_keys:
            word_list.extend(item["contraband_word"].split(","))
            print(str(item["id"]) + ":"+ str(item["contraband_word"]))
            mysql_instance.delete({"id":str(item["id"])})

        need_update = False
        if item["label"] == "":
            contraband_words = [item['text'][i[0]:i[1]] for i in check_label_keys]
            mysql_instance.update({ "contraband_word": ",".join(contraband_words)},
                                  {"id": str(item["id"])})
        else:
            label_list = ast.literal_eval(item['label'] )
            new_label = []
            for sub_label  in label_list:
                check_key = (sub_label[0],sub_label[1])
                if check_key not in check_label_keys:
                    need_update = True
                    continue
                new_label.append(sub_label)

            if need_update:
                contraband_words = [i[2] for i in new_label ]
                new_label.sort(key=lambda x:x[1])
                mysql_instance.update({"label":new_label,"contraband_word":",".join(contraband_words)},{"id":str(item["id"])})


    except Exception as e:
        print("http error")
    print(str(item["id"])+" over")


mysql_biaozhu_df = mysql_instance.select(["id","text","label","contraband_word"],{})

mysql_biaozhu_df.apply(get_doccano_result,axis=1)
print(list(set(word_list)))



